package com.ruoyi.web.controller.system.Fdongbei;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.Fdongbei.domain.TbGuozhong;
import com.ruoyi.system.Fdongbei.domain.TbMerge;
import com.ruoyi.system.Fdongbei.domain.dibang.GuozhongDo;
import com.ruoyi.system.Fdongbei.domain.dibang.TbMMGuozhong;
import com.ruoyi.system.Fdongbei.domain.vo.reqvo.CarInfoReqVo;
import com.ruoyi.system.Fdongbei.service.ITbMergeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 多交货单Controller
 * 
 * @author ruoyi
 * @date 2025-02-07
 */
@RestController
@RequestMapping("/system/merge")
public class TbMergeController extends BaseController
{
    @Autowired
    private ITbMergeService tbMergeService;

    /**
     * 查询多交货单列表
     */
    @PreAuthorize("@ss.hasPermi('system:merge:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbMerge tbMerge)
    {
        startPage();
        List<TbMerge> list = tbMergeService.selectTbMergeList(tbMerge);
        return getDataTable(list);
    }


    /**
     * 查询过重列表YLF数据
     */
    @PreAuthorize("@ss.hasPermi('system:merge:list')")
    @GetMapping("/listGuozhong")
    public TableDataInfo list(TbMMGuozhong tbGuozhong)
    {
        startPage();
        List<TbMMGuozhong> list = tbMergeService.selectTbMergeListGuozhong(tbGuozhong);
        return getDataTable(list);
    }

    /**
     * 导出多交货单列表
     */
<<<<<<< HEAD
    @PreAuthorize("@ss.hasPermi('system:merge:list')")
=======
    @PreAuthorize("@ss.hasPermi('system:merge:export')")
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
    @Log(title = "多交货单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbMerge tbMerge)
    {
        List<TbMerge> list = tbMergeService.selectTbMergeList(tbMerge);
        ExcelUtil<TbMerge> util = new ExcelUtil<TbMerge>(TbMerge.class);
        util.exportExcel(response, list, "多交货单数据");
    }

    /**
     * 获取多交货单详细信息
     */
<<<<<<< HEAD
    @PreAuthorize("@ss.hasPermi('system:merge:list')")
=======
    @PreAuthorize("@ss.hasPermi('system:merge:query')")
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
    @GetMapping(value = "/{czbh}")
    public AjaxResult getInfo(@PathVariable("czbh") String czbh)
    {
        return success(tbMergeService.selectTbMergeByCzbh(czbh));
    }

    /**
     * 新增多交货单
     */
<<<<<<< HEAD
    @PreAuthorize("@ss.hasPermi('system:merge:list')")
=======
    @PreAuthorize("@ss.hasPermi('system:merge:add')")
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
    @Log(title = "多交货单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbMMGuozhong tbMMGuozhong)
    {
        return toAjax(tbMergeService.insertTbMerge(tbMMGuozhong));
    }

    /**
     * 更新打印次数
     */
<<<<<<< HEAD
    @PreAuthorize("@ss.hasPermi('system:merge:list')")
=======
    @PreAuthorize("@ss.hasPermi('system:merge:edit')")
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
    @Log(title = "多交货单", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/dd/{czbh}")
    public AjaxResult edit(@PathVariable String czbh)
    {
        int req = tbMergeService.updateTbMergeDb(czbh);
        return toAjax(req);
    }
    /**
     * 多交货单非首次录入
     */
<<<<<<< HEAD
    @PreAuthorize("@ss.hasPermi('system:merge:list')")
=======
    @PreAuthorize("@ss.hasPermi('system:merge:edit')")
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
    @Log(title = "多交货单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateDD(@RequestBody TbMMGuozhong tbMMGuozhong)
    {
<<<<<<< HEAD
        String chehao = tbMMGuozhong.getChehao().replaceAll("\\.","");
=======

        System.out.println(tbMMGuozhong.getChehao()+"<-------tbMMGuozhong.getChehao()---------");
        String chehao = tbMMGuozhong.getChehao().replaceAll("\\.","");

        System.out.println(chehao+"<--------------chehao------------");
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
        if (!(chehao.equals(tbMMGuozhong.getChehaomm()))){
            return error("车号不一致，请检查车号信息");
        }
        int req = tbMergeService.updateTbMerge(tbMMGuozhong);
        if (req == 0) return  error("未查到对应的称重编号和车号或者客户名称不一致，请检查确认！");
        return toAjax(req);
    }
<<<<<<< HEAD


    /**
     * gengxin
     */
    @PreAuthorize("@ss.hasPermi('system:merge:xiugai')")
    @Log(title = "多交货单", businessType = BusinessType.UPDATE)
    @PutMapping("/gengxin")
    public AjaxResult update(@RequestBody TbMerge tbMerge)
    {
        int req = tbMergeService.updateTbMergewh(tbMerge);
        return toAjax(req);
    }
    /**
     * 删除多交货单
     */
    @PreAuthorize("@ss.hasPermi('system:merge:shanchu')")
=======
    /**
     * 删除多交货单
     */
    @PreAuthorize("@ss.hasPermi('system:merge:remove')")
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
    @Log(title = "多交货单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{czbhs}")
    public AjaxResult remove(@PathVariable String[] czbhs)
    {
<<<<<<< HEAD
        System.out.println("--------------->"+czbhs[0]);
        TbMerge merge = tbMergeService.selectTbMergeByCzbh(czbhs[0]);
        if (merge.getGbzt().equals("待进厂") || merge.getGbzt().equals("已进厂")){
            return toAjax(tbMergeService.deleteTbMergeByCzbhs(czbhs));
        }
        return error("车辆不为 待进厂 或 已进厂 无法删除！");
    }



    /**
     * 过磅状态退回
     */
    @PreAuthorize("@ss.hasPermi('system:merge:tuihui')")
    @Log(title = "多交货单", businessType = BusinessType.UPDATE)
    @PutMapping("/gbztth")
    public AjaxResult updateGbztth(@RequestBody TbMerge tbMerge)
    {
        String gbzt = tbMerge.getGbzt();
        switch (gbzt) {
            case "已进厂":
                tbMerge.setGbzt("待进厂");
                return toAjax(tbMergeService.updateTbMergewh(tbMerge));
            case "进厂称重":
                tbMerge.setGbzt("已进厂");
                tbMerge.setMz("0");
                return toAjax(tbMergeService.updateTbMergewh(tbMerge));
            case "卸车审核":
                tbMerge.setGbzt("进厂称重");
                return toAjax(tbMergeService.updateTbMergewh(tbMerge));
            case "卸车确认":
                tbMerge.setGbzt("卸车审核");
                return toAjax(tbMergeService.updateTbMergewh(tbMerge));
            default:
                return error("该车辆为 " + tbMerge.getGbzt() + " 无法进行状态回退。");
        }
    }

    /**
     * 退车
     */
    @PreAuthorize("@ss.hasPermi('system:merge:tuiche')")
    @Log(title = "退车", businessType = BusinessType.UPDATE)
    @PutMapping("/yj")
    public AjaxResult updateYj(@RequestBody TbMerge tbMerge)
    {
        String yj = tbMerge.getYj();
        if (yj.equals("退车")){
            tbMerge.setYj("正常");
            return toAjax(tbMergeService.updateTbMergewh(tbMerge));
        }else{
            tbMerge.setYj("退车");
            return toAjax(tbMergeService.updateTbMergewh(tbMerge));
        }
    }

    /**
     * 复磅
     */
    @PreAuthorize("@ss.hasPermi('system:merge:fubang')")
    @Log(title = "复磅", businessType = BusinessType.UPDATE)
    @PutMapping("/fb")
    public AjaxResult updatefb(@RequestBody TbMerge tbMerge)
    {
        Long fb = tbMerge.getClFbbz();
        if (fb == 1){
            tbMerge.setClFbbz((long) 0);
            return toAjax(tbMergeService.updateTbMergewh(tbMerge));
        }else{
            return error("不为复磅车辆！！");
        }
    }

    /**
     *  完成未出场门禁修改
     */
    @PreAuthorize("@ss.hasPermi('system:merge:menjin')")
    @Log(title = "menjin", businessType = BusinessType.UPDATE)
    @PutMapping("/menjin")
    public AjaxResult updatemenjin(@RequestBody TbMerge tbMerge)
    {
        return tbMergeService.menjinModifyStatus(tbMerge);
=======
        return toAjax(tbMergeService.deleteTbMergeByCzbhs(czbhs));
>>>>>>> b4a0e2dba4b1f1e0b05e6d155df4272830d06f27
    }
}
